package com.lms.vo;

import com.lms.bean.Book;
import com.lms.bean.Type;
import org.springframework.beans.BeanUtils;

/**
 * 视图层对象(View Object)
 * 用于传输到前端页面
 * 为了避免转化成JSON时陷入无穷递归链，所有的的视图层对象(VO)中都不能包含对象字段(例如Book,User)
 */
//RestFul 风格接口        java 对象  ->  JSON
public class BookVO extends Book {
    private String bookType;
    private Long bookTypeId;

    /**
     * 将 Bean 转化成 VO
     */
    public static BookVO convert(Book book) {
        BookVO bookVO = new BookVO();
        BeanUtils.copyProperties(book, bookVO);
        Type type = book.getType();

        //用  bookType  bookTypeId 这两个字段 替换 type 对象 ,去除 rentRecords returnRecords这两个对象
        bookVO.setRentRecords(null);
        bookVO.setReturnRecords(null);
        bookVO.setType(null);
        if (type != null) {
            bookVO.setBookType(type.getTypename());
            bookVO.setBookTypeId(type.getTypeId());
        }
        return bookVO;
    }

    public String getBookType() {
        return bookType;
    }

    public void setBookType(String bookType) {
        this.bookType = bookType;
    }

    public Long getBookTypeId() {
        return bookTypeId;
    }

    public void setBookTypeId(Long bookTypeId) {
        this.bookTypeId = bookTypeId;
    }
}
